<template>
  <div class="content">
    <div class="header">
      <h3 class="title">{{ props.contentConfig.header?.title }}</h3>
      <el-button v-if="isCreate" type="primary" @click="handleNewClick">
        {{ props.contentConfig.header?.btnTitle }}
      </el-button>
    </div>
    <div class="table">
      <el-table
        :data="pageList"
        border
        style="width: 100%"
        :row-key="contentConfig?.childrenTree?.rowKey ?? ''"
      >
        <template
          v-for="item in props.contentConfig.propsList"
          :key="item.prop"
        >
          <template v-if="item.type === 'timer'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope">
                {{ formatDate(scope.row[item.prop]) }}
              </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'handler'">
            <el-table-column align="center" v-bind="item">
              <template #default="scope">
                <el-button
                  v-if="isUpdate"
                  type="primary"
                  size="small"
                  icon="Edit"
                  text
                  @click="handleEditClick(scope.row)"
                  >编辑</el-button
                >
                <el-button
                  v-if="isDelete"
                  type="danger"
                  size="small"
                  icon="Delete"
                  text
                  @click="handleDeteleClick(scope.row.id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </template>
          <template v-else-if="item.type === 'custom'">
            <el-table-column align="center" v-bind="item">
              <!-- 自定义具名插槽 -->
              <template #default="scope">
                <slot :name="item.slotName" v-bind="scope" :prop="item.prop">
                </slot>
              </template>
            </el-table-column>
          </template>
          <template v-else>
            <el-table-column align="center" v-bind="item"> </el-table-column>
          </template>
        </template>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { useSystemStore } from '@/stores/main/system/stytem'
import { storeToRefs } from 'pinia'
import { formatDate } from '@/utils/format'
import { ref } from 'vue'
import { usePermissions } from '@/hooks/usePermissions'

interface IProp {
  contentConfig: {
    pageName: string
    header?: {
      title?: string
      btnTitle?: string
    }
    propsList: any[]
    childrenTree?: any
  }
}
const emit = defineEmits(['newClick', 'editClick'])
const props = defineProps<IProp>()

// 判断是否有权限
const isCreate = usePermissions(`${props.contentConfig.pageName}:create`)
const isDelete = usePermissions(`${props.contentConfig.pageName}:delete`)
const isUpdate = usePermissions(`${props.contentConfig.pageName}:update`)
const isQuery = usePermissions(`${props.contentConfig.pageName}:query`)

// 发送action请求
const systemStore = useSystemStore()
const currentPage = ref(1)
const pageSize = ref(10)
fetchPagePageData()

// 获取数据
const { pageList, pageTotalCount } = storeToRefs(systemStore)
// 分页参数

function handleCurrentChange() {
  fetchPagePageData()
}
function handleSizeChange() {
  fetchPagePageData()
}

// 分页数据请求方法
function fetchPagePageData(formData?: any) {
  // 没有查询权限
  if (!isQuery) return
  // 获取offset、size
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset }
  const info = { ...pageInfo, ...formData }
  // 发起网络请求
  systemStore.postPageListAction(props.contentConfig.pageName, info)
}

// 删除
function handleDeteleClick(id: number) {
  systemStore.deletePageById(props.contentConfig.pageName, id)
}
// 编辑
function handleEditClick(data: any) {
  emit('editClick', data)
}
// 新建
function handleNewClick() {
  emit('newClick')
}

// 暴露方法让父组件调用
defineExpose({ fetchPagePageData })
</script>

<style lang="scss" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;
  .title {
    font-size: 22px;
  }
}
.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 15px;
}
</style>
